/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ventas.java
 *
 * Created on 06-mar-2012, 12:33:57
 */

package desktop;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author KrooS
 */
public class Ventas extends javax.swing.JDialog {
    Conex conexion;
    String Query = "select * from recetas_pendientes where pedido=1 and rut_cliente=";
    String Query1 = "select * from recetas_pendientes where pedido=1 and id_Receta=";
    String Query3= "select nombre,cant_prod,id_prod_pend from producto_pendiente where id_receta=";
    int col,fil;
    Object editar= null;
    Object id_boleta;
    Object producto= null;

    public String rut;
    public String id_prod;
    ResultSetTableModel modelo;
    ResultSetTableModel prods;

    /** Creates new form Ventas */
    public Ventas(java.awt.Frame parent, boolean modal) throws ClassNotFoundException, SQLException {
        super(parent, modal);
        initComponents();
        conexion = new Conex();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo_vnt = new javax.swing.ButtonGroup();
        panel_vnt = new javax.swing.JPanel();
        jTextField1_vnt = new javax.swing.JTextField();
        jRadioButton1_vnt = new javax.swing.JRadioButton();
        jRadioButton2_vnt = new javax.swing.JRadioButton();
        jTextField2_vnt = new javax.swing.JTextField();
        digito_vnt = new javax.swing.JTextField();
        jLabel2_vnt = new javax.swing.JLabel();
        buscar_vnt = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1_vnt = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        titulo_vnt = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panel_vnt.setBackground(new java.awt.Color(156, 203, 216));

        jTextField1_vnt.setEditable(false);

        jRadioButton1_vnt.setBackground(new java.awt.Color(156, 203, 216));
        grupo_vnt.add(jRadioButton1_vnt);
        jRadioButton1_vnt.setText("Ingrese Código Vnt.");
        jRadioButton1_vnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1_vntActionPerformed(evt);
            }
        });

        jRadioButton2_vnt.setBackground(new java.awt.Color(156, 203, 216));
        grupo_vnt.add(jRadioButton2_vnt);
        jRadioButton2_vnt.setText("Ingrese Rut Cliente");
        jRadioButton2_vnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2_vntActionPerformed(evt);
            }
        });

        jTextField2_vnt.setEditable(false);
        jTextField2_vnt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField2_vntKeyTyped(evt);
            }
        });

        digito_vnt.setEditable(false);
        digito_vnt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                digito_vntKeyTyped(evt);
            }
        });

        jLabel2_vnt.setText("-");

        buscar_vnt.setText("Buscar");
        buscar_vnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscar_vntActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "id Receta", "Fecha ingreso", "Rut Medico", "Rut Cliente", "Pedido"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setResizingAllowed(false);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1_vnt.setText("Editar");
        jButton1_vnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1_vntActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Producto", "Cantidad", "Id Prod Pend"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.getTableHeader().setResizingAllowed(false);
        jTable2.getTableHeader().setReorderingAllowed(false);
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jButton1.setText("Comprar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel_vntLayout = new javax.swing.GroupLayout(panel_vnt);
        panel_vnt.setLayout(panel_vntLayout);
        panel_vntLayout.setHorizontalGroup(
            panel_vntLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_vntLayout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jRadioButton1_vnt)
                .addGap(18, 18, 18)
                .addComponent(jTextField1_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jRadioButton2_vnt)
                .addGap(18, 18, 18)
                .addComponent(jTextField2_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2_vnt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(digito_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addComponent(buscar_vnt)
                .addGap(45, 45, 45))
            .addGroup(panel_vntLayout.createSequentialGroup()
                .addGap(225, 225, 225)
                .addComponent(jButton1_vnt)
                .addGap(121, 121, 121)
                .addComponent(jButton1)
                .addContainerGap(218, Short.MAX_VALUE))
            .addGroup(panel_vntLayout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(172, Short.MAX_VALUE))
            .addGroup(panel_vntLayout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(103, Short.MAX_VALUE))
        );
        panel_vntLayout.setVerticalGroup(
            panel_vntLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_vntLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(panel_vntLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1_vnt)
                    .addComponent(jRadioButton2_vnt)
                    .addComponent(jTextField2_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2_vnt)
                    .addComponent(digito_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscar_vnt))
                .addGap(39, 39, 39)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(panel_vntLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1_vnt)
                    .addComponent(jButton1))
                .addContainerGap(100, Short.MAX_VALUE))
        );

        titulo_vnt.setFont(new java.awt.Font("Tahoma", 1, 18));
        titulo_vnt.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titulo_vnt.setText("Venta de Recetas Medicas");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel_vnt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(241, 241, 241)
                        .addComponent(titulo_vnt)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(titulo_vnt, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panel_vnt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-730)/2, (screenSize.height-595)/2, 730, 595);
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton1_vntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1_vntActionPerformed
        // TODO add your handling code here:
        this.jTextField2_vnt.setEditable(false);
        this.jTextField2_vnt.setText("");
        this.digito_vnt.setEditable(false);
        this.digito_vnt.setText("");
        this.jTextField1_vnt.setEditable(true);

    }//GEN-LAST:event_jRadioButton1_vntActionPerformed

    private void jTextField2_vntKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2_vntKeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
            evt.consume();  // ignorar el evento de teclado
        }
}//GEN-LAST:event_jTextField2_vntKeyTyped

    private void digito_vntKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_digito_vntKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_digito_vntKeyTyped

    private void jRadioButton2_vntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2_vntActionPerformed
        // TODO add your handling code here:
        this.jTextField1_vnt.setEditable(false);
        this.jTextField1_vnt.setText("");
        this.jTextField2_vnt.setEditable(true);
        this.digito_vnt.setEditable(true);
    }//GEN-LAST:event_jRadioButton2_vntActionPerformed

    private void buscar_vntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscar_vntActionPerformed
        // TODO add your handling code here:
        if(this.jRadioButton1_vnt.isSelected() && !this.jTextField1_vnt.getText().isEmpty())
    {
            try {
                String id = this.jTextField1_vnt.getText();
                String busqueda_receta_pnd = Query1+id;
                modelo = new ResultSetTableModel(Conex.CONTROLADOR, Conex.URL_BASEDATOS, Conex.NOMBRE_USUARIO, Conex.CONTRA, busqueda_receta_pnd);
                this.jTable1.setModel(modelo);
            } catch (SQLException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

        if(this.jRadioButton2_vnt.isSelected() && !this.jTextField2_vnt.getText().isEmpty())
    {
            try {
                String run = this.jTextField2_vnt.getText();
                String dig = this.digito_vnt.getText();
                String rut_completo = run+"-"+dig;
                String recetas_rut = Query+"'"+rut_completo+"'";
                modelo = new ResultSetTableModel(Conex.CONTROLADOR, Conex.URL_BASEDATOS, Conex.NOMBRE_USUARIO, Conex.CONTRA, recetas_rut);
                this.jTable1.setModel(modelo);
            } catch (SQLException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
        
    }//GEN-LAST:event_buscar_vntActionPerformed

    private void jButton1_vntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1_vntActionPerformed
        // TODO add your handling code here:
        if(this.editar!= null && col!=2)
    {
        String new_Dat=JOptionPane.showInputDialog(null, "Ingrese Dato", "Editar", JOptionPane.INFORMATION_MESSAGE);
        if(col==1)
        {
            int new_num= Integer.parseInt(new_Dat);
            System.out.println(new_num);

                try {
                    conexion.editar_prod_vnt_cantidad("cant_prod", producto, new_num);
                } catch (SQLException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                }
            this.jTable2.setModel(prods);
            this.jTable2.repaint();
        
        }
        if(col==0)
        {
            System.out.println(new_Dat);
                try {
                    conexion.editar_prod_vnt_nombre( producto, new_Dat);
                } catch (SQLException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
       
    }
    else
    {
        JOptionPane.showMessageDialog(null, "no se puede editar...", "error", 2);
    }
    }//GEN-LAST:event_jButton1_vntActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int indice=this.jTable1.getSelectedRow();
        id_boleta =this.jTable1.getValueAt(indice, 0);
        System.out.println(id_boleta);

         try {

                String prod_pend = Query3+id_boleta;
                prods= new ResultSetTableModel(Conex.CONTROLADOR, Conex.URL_BASEDATOS, Conex.NOMBRE_USUARIO, Conex.CONTRA, prod_pend);
                this.jTable2.setModel(prods);
            } catch (SQLException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Productos.class.getName()).log(Level.SEVERE, null, ex);
            }

            this.rut= (String) this.jTable1.getValueAt(indice, 3);
            System.out.println(rut);

    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        // TODO add your handling code here:
         col=this.jTable2.getSelectedColumn();
         fil=this.jTable2.getSelectedRow();


    this.editar=this.jTable2.getValueAt(fil, col);
    this.producto=this.jTable2.getValueAt(fil, 2);
    }//GEN-LAST:event_jTable2MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            // TODO add your handling code here:
           // this.id_prod=

            Vnt_Final fin;
            fin = new Vnt_Final(this, true);
            try {
                fin.establece_tabla(id_boleta);
            } catch (SQLException ex) {
                Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
            }
            fin.pack();
            fin.Vnt = this;
            fin.setVisible(true);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Ventas dialog = new Ventas(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscar_vnt;
    private javax.swing.JTextField digito_vnt;
    private javax.swing.ButtonGroup grupo_vnt;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton1_vnt;
    private javax.swing.JLabel jLabel2_vnt;
    private javax.swing.JRadioButton jRadioButton1_vnt;
    private javax.swing.JRadioButton jRadioButton2_vnt;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    public javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1_vnt;
    private javax.swing.JTextField jTextField2_vnt;
    private javax.swing.JPanel panel_vnt;
    private javax.swing.JLabel titulo_vnt;
    // End of variables declaration//GEN-END:variables

}
